package org.dairy.farms.nandana.core.logic;

import org.dairy.farms.dao.ExpenseDAO;
import org.dairy.farms.dao.ExpenseTypeDAO;
import org.dairy.farms.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * User: gduggirala
 * Date: Aug 1, 2011
 * Time: 12:36:12 PM
 */
@Service("expenseManagementLogic")
public class ExpenseManagementLogicImpl implements ExpenseManagementLogic {
    private static final Logger logger = LoggerFactory.getLogger(ExpenseManagementLogicImpl.class);
    @Autowired
    private ExpenseTypeDAO expenseTypeDao;

    @Autowired
    private ExpenseDAO expenseDao;


    public List<ExpenseType> getExpenseTypes(final String expenseType) {
        return expenseTypeDao.findByProperty("expenseTypeOf", expenseType);
    }

    public List<Expense> getLivestockExpenses(Long livestockId, int page, int start, int limit) {
        List<Expense> livestockExpenseList = expenseDao.findByLivestockId(livestockId, page, start, limit);
        return livestockExpenseList;
    }

    @Override
    @Transactional
    public List<Expense> getLivestockExpenses(Long livestockId) {
        List<Expense> livestockExpenseList = expenseDao.findByLivestockId(livestockId);
        return filterByLivestockId(livestockId, livestockExpenseList);
    }

    public List<Expense> filterByLivestockId(Long livestockId, List<Expense> expenseList) {
        List<Expense> returnList = new ArrayList<Expense>();
       /* for (Expense expense : expenseList) {
            if (expense instanceof LivestockExpense && ((LivestockExpense) expense).getLivestock().getId().equals(livestockId)) {
                returnList.add(expense);
            }
            if (expense instanceof CheckupExpense && ((CheckupExpense) expense).getCheckup().getLivestock().getId().equals(livestockId)) {
                returnList.add(expense);
            }
            if (expense instanceof FeedExpense && ((FeedExpense) expense).getFeed().getLivestock().getId().equals(livestockId)) {
                returnList.add(expense);
            }
        }*/
        return returnList;
    }
}
